﻿using CNKI.TPI.Web.Admin.Model;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Search.IDAO;
using KNet.Data.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace CNKI.TPI.Web.Search.DAO
{
    public class TopicDAO : BaseDAO, ITopicDAO
    {
        public IList<SYS_CMS_TOPIC_PAGECONFIG> GetPageConfig(string topiccode)
        {
            IList<SYS_CMS_TOPIC_PAGECONFIG> result = null;
            try
            {
                ExpressionCombine<SYS_CMS_TOPIC_PAGECONFIG> exp = new ExpressionCombine<SYS_CMS_TOPIC_PAGECONFIG>();
                Expression<Func<SYS_CMS_TOPIC_PAGECONFIG, bool>> match = null; //查询条件
                exp.And("TopicCode", OperatorType.Equal, topiccode);
                exp.And("Status", OperatorType.Equal, 1);
                match = exp.Compile();

                result = Context.Get<SYS_CMS_TOPIC_PAGECONFIG>(match);
                IList<SYS_CMS_TOPIC_PAGECONFIG> list = Context.Get<SYS_CMS_TOPIC_PAGECONFIG>(exp.Compile());
                if (list != null && list.Count > 0)
                {
                    foreach (SYS_CMS_TOPIC_PAGECONFIG item in list)
                    {
                        item.TopicCode = StringHelper.RemoveHighlightFlag(item.TopicCode);
                    }
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        public IList<SYS_CMS_TOPIC_DBCONFIG> GetDBConfig(string sysid)
        {
            IList<SYS_CMS_TOPIC_DBCONFIG> result = null;
            try
            {
                ExpressionCombine<SYS_CMS_TOPIC_DBCONFIG> exp = new ExpressionCombine<SYS_CMS_TOPIC_DBCONFIG>();
                Expression<Func<SYS_CMS_TOPIC_DBCONFIG, bool>> match = null; //查询条件
                exp.And("SYSID", OperatorType.Equal, sysid);
                exp.And("Status", OperatorType.Equal, 1);
                match = exp.Compile();

                result = Context.Get<SYS_CMS_TOPIC_DBCONFIG>(match);
                IList<SYS_CMS_TOPIC_DBCONFIG> list = Context.Get<SYS_CMS_TOPIC_DBCONFIG>(exp.Compile());
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        public IList<SYS_CMS_TOPIC> GetTopicInfo(string topiccode)
        {
            IList<SYS_CMS_TOPIC> result = null;
            try
            {
                ExpressionCombine<SYS_CMS_TOPIC> exp = new ExpressionCombine<SYS_CMS_TOPIC>();
                Expression<Func<SYS_CMS_TOPIC, bool>> match = null; //查询条件
                exp.And("TopicCode", OperatorType.Equal, topiccode);
                exp.And("TopicStatus", OperatorType.Equal, 1);
                match = exp.Compile();

                result = Context.Get<SYS_CMS_TOPIC>(match);
                if (result != null && result.Count > 0)
                {
                    foreach (SYS_CMS_TOPIC item in result)
                    {
                        item.TopicCode = StringHelper.RemoveHighlightFlag(item.TopicCode);
                    }
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        public SYS_CMS_TOPIC_CLS_QUERY GetDBTopicQuery(string dbCode, string clsCode, string nodeCode)
        {
            SYS_CMS_TOPIC_CLS_QUERY result = null;
            try
            {
                ExpressionCombine<SYS_CMS_TOPIC_CLS_QUERY> exp = new ExpressionCombine<SYS_CMS_TOPIC_CLS_QUERY>();
                exp.And("DBCode", OperatorType.Equal, dbCode);
                exp.And("NodeCode", OperatorType.Equal, nodeCode);
                exp.And("CLSCode", OperatorType.Equal, clsCode);
                exp.And("Status", OperatorType.Equal, 1);

                IList<SYS_CMS_TOPIC_CLS_QUERY> list = Context.Get<SYS_CMS_TOPIC_CLS_QUERY>(exp.Compile());
                if (list != null && list.Count > 0)
                {
                    result = list[0];

                    result.DBCode = StringHelper.RemoveHighlightFlag(result.DBCode);
                    result.NodeCode = StringHelper.RemoveHighlightFlag(result.NodeCode);
                    result.CLSCode = StringHelper.RemoveHighlightFlag(result.CLSCode);
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        public IList<SYS_CMS_TOPIC> GetTopicInfoAll()
        {
            IList<SYS_CMS_TOPIC> result = null;
            try
            {
                result = Context.GetAll<SYS_CMS_TOPIC>();
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
    }
}
